package tech.tongyu.bct.controller;

import tech.tongyu.bct.common.cache.service.CacheService;
import tech.tongyu.bct.service.TestCacheService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author summit
 * @since 2021/7/14 19:11
 */
@RestController("cache")
public class CacheController {

	@Autowired
	private TestCacheService testCacheService;

	@Autowired
	private CacheService cacheService;

	@GetMapping("cache")
	@Cacheable(cacheNames = "test",key = "#name")
	public String cache(String name) {
		return "hello cache";
	}

	@GetMapping("evit")
	public String evit(@RequestParam(required = false) String name) {
		testCacheService.add(name);
		return "evit";
	}

	@GetMapping("add")
	public String add(@RequestParam(required = false) String name) {
		testCacheService.set(name);
		return "evit";
	}

	@GetMapping("cacheEvit")
	public String cacheEvit(String cacheName) {
		cacheService.clear(cacheName);
		return "cacheEvit";
	}

}
